<?php

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

namespace EasyActionPHP\Dispatcher;

use EasyActionPHP\Core as Core;
use EasyActionPHP\Controller as Controller;

/**
 * Camada de mais alto nível da camada de visão.
 *
 * @author Flavio de Vasconcellos Correa
 * @since 2007/06/04
 * @package EasyActionPHP
 * @subpackage Dispatcher
 * @version $id$
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 */
class ActionTemplateSingleton {

    /**
     * @staticvar ActionTemplateSingleton Instancia Singleton da classe.
     */
    static $instance;

    /**
     * @var Smarty Instancia para a Smarty Template Engine.
     */
    private $smarty;

    /**
     * @var HttpResponse Instancia das respostas da aplicação.
     */
    private $response;

    /**
     * Construtor.
     * @access private
     */
    private function __construct() {
        $this->smarty = & new \Smarty();
        $this->smarty->template_dir = Core\Application::getParamByName('SMARTY-TEMPLATE-DIR');
        $this->smarty->cache_dir = Core\Application::getParamByName('SMARTY-CACHE-DIR');
        $this->smarty->compile_dir = Core\Application::getParamByName('SMARTY-COMPILE-DIR');
        //$this->smarty->plugins_dir[] = Application::getParamByName('SMARTY-PLUGINS-DIR');
        foreach (Core\Application::getParamByName('SMARTY-PLUGINS-DIR') as $dir)
            $this->smarty->plugins_dir[] = $dir;
    }

    /**
     * Atualiza aplicação.
     * @access private
     * @return void
     */
    private function refresh() {
        if ($this->response instanceof Controller\HttpResponse) {
            $application = Core\Application::getInstance();
            foreach ($application->getParamList() as $name => $value)
                $this->smarty->assign($name, $value);
            foreach ($this->response->getAttributes() as $name => $value)
                $this->smarty->assign($name, $value);
            $this->smarty->assign('SESSION', $_SESSION);
        }
    }

    /**
     * Retorna a instância Singleton da classe.
     *
     * @access public
     * @return ActionTemplateSingleton instancia Singleton
     */
    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new ActionTemplateSingleton();
        }
        return(self::$instance);
    }

    /**
     * Envia para saída o resultado entre o modelo e a resposta da aplicação.
     *
     * @access public
     * @return void
     */
    public function display($template, &$response) {
        echo(self::fetch($template, $response));
    }

    /**
     * Retorna o resultado entre o modelo e a resposta da aplicação.
     *
     * @access public
     * @return String
     */
    public static function fetch($template, &$response) {
        $instance = self::getInstance();
        $instance->response = $response;
        $instance->refresh();
        return($instance->smarty->fetch($template));
    }

}

?>